package de.dopichaj.labrador.tools;


import java.io.DataInput;
import java.io.EOFException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Map;

import de.dopichaj.labrador.index.backend.TermFrequency;
import de.dopichaj.labrador.index.backend.TermFrequencyInputStream;
import de.dopichaj.labrador.util.CountingMap;
import de.dopichaj.labrador.util.io.BitInputStream;
import de.dopichaj.labrador.util.io.BitOutputStream;
import de.dopichaj.labrador.util.io.huffman.HuffmanTree;


public final class TermFrequencyHuffman {

    private TermFrequencyHuffman(final RandomAccessFile input,
        final BitOutputStream output) throws IOException {
        
        // build the Huffman tree
        final HuffmanTree<Integer> tree = createTree(input);
        
        // write the compressed version
    }
    
    private HuffmanTree<Integer> createTree(final DataInput input) throws IOException {
        
        final TermFrequencyInputStream tfInput =
            new TermFrequencyInputStream(new BitInputStream(input), true, 0);
        // create the count map
        final CountingMap<Integer> countMap = new CountingMap<Integer>();
        final CountingMap<Integer> docIDCountMap = new CountingMap<Integer>();
        
        // until we run out of runs
        try {
            while (true) {
                
                // set the previous document ID to 0
                int previousDocumentID = 0;
                
                // read the next term frequency, while the run is not at the end
                TermFrequency freq;
                while ((freq = tfInput.readTermFrequency()) != null) {
    
                    // increase the count corresponding to the term frequency
                    countMap.count(freq.getFrequency());
                    
                    // increase the count corresponding to the docID delta
                    assert freq.getDocumentID() > previousDocumentID : previousDocumentID + " >= " + freq;
                    docIDCountMap.count(freq.getDocumentID() - previousDocumentID);
                    
                    // update the previous document ID
                    previousDocumentID = freq.getDocumentID();
                }
                
                // skip to the next run
                tfInput.skipToNext();
            }
        } catch (EOFException e) {
            // do nothing
        }
        
        for (final Map.Entry<Integer, Integer> entry : countMap.entrySet()) {
            System.err.println(entry.getKey() + "\t" + entry.getValue());
        }
        
        final HuffmanTree<Integer> tfTree = new HuffmanTree<Integer>(countMap);
        System.out.print(tfTree);
        System.out.println("===");
        final HuffmanTree<Integer> docIDTree = new HuffmanTree<Integer>(docIDCountMap);
        System.out.print(docIDTree);
        return tfTree;
    }
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        new TermFrequencyHuffman(new RandomAccessFile(args[0], "r"), null);
    }

}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/